from django import forms
from students.models import Student, StudentDetail


class LoginForm(forms.Form):
    username = forms.CharField(
        label='用户名',
        max_length=10,
        min_length=3,
        required=True,
        widget=forms.TextInput(attrs={
            'class': 'form-control form-control-lg',
            'placeholder': '请输入用户名',
        }),
        error_messages={
            'max_length': '长度不能超过10个字符',
            'min_length': '长度不能少于3个字符',
            'required': '用户名不能为空',
        }
    )
    password = forms.CharField(
        label='密码',
        max_length=20,
        min_length=6,
        required=True,
        widget=forms.PasswordInput(attrs={
            'class': 'form-control form-control-lg',
            'placeholder': '请输入密码',
        }),
        error_messages={
            'max_length': '长度不能超过8个字符',
            'min_length': '长度不能少于6个字符',
            'required': '密码不能为空',
        }
    )


class RegisterForm(forms.Form):
    username = forms.CharField(
        label='用户名',
        max_length=10,
        min_length=3,
        required=True,
        widget=forms.TextInput(attrs={
            'class': 'form-control form-control-lg',
            'placeholder': '请输入长度为3-10位英文的用户名',
        }),
        error_messages={
            'max_length': '长度不能超过10个字符',
            'min_length': '长度不能少于3个字符',
            'required': '用户名不能为空',
        }
    )
    password = forms.CharField(
        label='密码',
        max_length=20,
        min_length=6,
        required=True,
        widget=forms.PasswordInput(attrs={
            'class': 'form-control form-control-lg',
            'placeholder': '请输入长度为6-20位数字或字母的密码',
        }),
        error_messages={
            'max_length': '长度不能超过8个字符',
            'min_length': '长度不能少于6个字符',
            'required': '密码不能为空',
        }
    )
    password2 = forms.CharField(
        label='确认密码',
        widget=forms.PasswordInput(attrs={
            'class': 'form-control form-control-lg',
            'placeholder': '请确认密码',
        }),
    )

    # 校验单个字段
    # def clean_username(self):

    # 校验多个字段
    def clean(self):
        cleand_data = super().clean()  # 原本的校验要保留，就需要继承
        password = cleand_data.get('password')
        password2 = cleand_data.get('password2')
        if password != password2:
            self.add_error('password2', '密码不一致')

# class DetailForm(forms.Form):
#     num = forms.CharField()
#     name = forms.CharField()
#     ...
#     college = forms.CharField()

class StudentForm(forms.ModelForm):
    class Meta:
        model = Student
        exclude = ['is_delete']
        widgets = {
            'sex': forms.RadioSelect
        }


class StudentDetailForm(forms.ModelForm):
    class Meta:
        model = StudentDetail
        exclude = ['student']


    def clean_num(self):
        data = self.cleaned_data.get('num')
        # 除了最后一位以外，都是数字类型的
        if not data[:-1].isdigit():
            # 添加错误信息
            self.add_error('num', '您输入的身份证号码不符合规则')
        return data # 要记得返回数据
